﻿<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 规程列表 -->
      <el-col :lg="24" :xs="24">
        <!-- 搜索区域 -->
        <div class="search-container">
          <el-form :inline="true" label-width="auto">
            <el-form-item label="规程名称">
              <el-input v-model="paramRegulationName" />
            </el-form-item>

            <el-form-item class="search-buttons">
              <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
              <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="hover" class="data-table">
          <div class="data-table__toolbar">
            <div class="data-table__toolbar--actions">
              <el-button type="success" icon="plus" @click="handleOpenDialog()">新增</el-button>
            </div>
          </div>

          <!-- 规程列表 -->
          <el-table
            v-loading="loading"
            border
            stripe
            :data="tableData"
            highlight-current-row
            class="data-table__content"
          >
            <el-table-column label="序号" width="60px" type="index" align="center" />
            <el-table-column label="规程名称" align="center" prop="name" />
            <el-table-column label="检定点" align="center" prop="pointsStr" />
            <el-table-column label="允许误差(±)" align="center" prop="errorFormula" />
            <el-table-column label="读取次数" align="center" prop="readCount" />
            <el-table-column label="读数间隔(s)" align="center" prop="readIntervalSecond" />
            <el-table-column label="稳定时间(s)" align="center" prop="stableSeconds" />
            <el-table-column label="稳定范围(±)" align="center" prop="stableErrorFormula" />
            <el-table-column label="偏移量(±)" align="center" prop="stableOffsetFormula" />
            <el-table-column label="合格判定" align="center" prop="qualifiedJudgment">
              <template #default="scope">
                {{ EnumQualifiedJudgment[scope.row.qualifiedJudgment] }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right">
              <template #default="scope">
                <el-button
                  type="primary"
                  icon="edit"
                  link
                  size="small"
                  @click="handleOpenDialog(scope.row)"
                >
                  编辑
                </el-button>
                <el-button
                  type="danger"
                  icon="delete"
                  link
                  size="small"
                  @click="handleDelete(scope.row.id)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 规程列表 end -->
        </el-card>
      </el-col>
    </el-row>

    <!-- 配置详情弹窗 -->
    <el-dialog
      v-if="dialogDetailVisible"
      v-model="dialogDetailVisible"
      width="700"
      :title="dialogTitle"
    >
      <RegulationDetail :form-data="dialogDetailData" @close="handleCloseDialog"></RegulationDetail>
    </el-dialog>
    <!-- 配置详情弹窗 End -->
  </div>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
import { Regulation } from "@/types/business/regulation";
import regulationApi from "@/api/business/regulation.api";
import { EnumQualifiedJudgment } from "@/enums";
import RegulationDetail from "@/views/business/regulation/components/RegulationDetail.vue";
import { deepClone } from "@/utils";

const loading = ref(false);
// 查询参数：规程名称
const paramRegulationName = ref<string>();

// 表格数据
const tableData = ref<Regulation[]>();
const dialogDetailVisible = ref(false);
const dialogDetailData = ref<Regulation>();
const dialogTitle = ref("");

onMounted(() => {
  handleQuery();
});

// 查询方法
function handleQuery() {
  loading.value = true;

  regulationApi.getList(paramRegulationName.value).then((data) => {
    tableData.value = data;
  });

  loading.value = false;
}
function handleResetQuery() {
  paramRegulationName.value = undefined;
  handleQuery();
}
function handleOpenDialog(model?: Regulation) {
  let currentData = model ? deepClone(model) : {};

  dialogTitle.value = model ? "编辑规程" : "新增规程";
  if (model === undefined) {
    regulationApi.getOrDefault().then((data) => {
      currentData = data;
      dialogDetailData.value = { ...currentData };
      dialogDetailVisible.value = true;
    });
  } else {
    dialogDetailData.value = { ...currentData };
    dialogDetailVisible.value = true;
  }
}
function handleDelete(id: number) {
  ElMessageBox.confirm("确认删除该规程？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    regulationApi.delete(id).then(() => {
      ElMessage.success("删除成功");
      handleQuery();
    });
  });
}

function handleCloseDialog(refresh: boolean) {
  dialogDetailVisible.value = false;
  if (refresh) handleQuery();
}
</script>

<style scoped lang="scss"></style>
